package com.ruoyi.controller;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.domain.ZhwyHirer;
import com.ruoyi.domain.ZhwyRentalcontract;
import com.ruoyi.domain.ZhwyShops;
import com.ruoyi.mapper.ZhwyHirerMapper;
import com.ruoyi.mapper.ZhwyRentalcontractMapper;
import com.ruoyi.mapper.ZhwyShopsMapper;
import com.ruoyi.mapper.ZhwyTemporarydepositMapper;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.domain.ZhwyTemporarydeposit;
import com.ruoyi.service.IZhwyTemporarydepositService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 临时收费/押金管理Controller
 * 
 * @author liuzhiqiang
 * @date 2024-05-07
 */
@RestController
@RequestMapping("/feeDeposit/temporarydeposit")
public class ZhwyTemporarydepositController extends BaseController
{
    @Autowired
    private IZhwyTemporarydepositService zhwyTemporarydepositService;
    @Autowired
    private ZhwyRentalcontractMapper zhwyRentalcontractMapper;
    @Autowired
    private ZhwyHirerMapper zhwyHirerMapper;
    @Autowired
    private ZhwyTemporarydepositMapper zhwyTemporarydepositMapper;
    /**
     * 查询临时收费
     */
    @PreAuthorize("@ss.hasPermi('feeDeposit:temporarydeposit:list')")
    @GetMapping("/list")
    public TableDataInfo list(ZhwyTemporarydeposit zhwyTemporarydeposit)
    {
        startPage();
        List<ZhwyTemporarydeposit> list = zhwyTemporarydepositService.selectZhwyTemporarydepositList(zhwyTemporarydeposit);
        if(zhwyTemporarydeposit.getStartTime()!=null && zhwyTemporarydeposit.getEndTime()!=null){
            List<ZhwyTemporarydeposit> list1 = new ArrayList<>();
            for (ZhwyTemporarydeposit temporarydeposit : list) {
                Date startTime = new Date(temporarydeposit.getStartTime());
                Date endTime = new Date(temporarydeposit.getEndTime());
                if(temporarydeposit.getChargeTime().after(startTime)&&temporarydeposit.getChargeTime().before(endTime)){
                    list1.add(temporarydeposit);
                }
            }
            return getDataTable(list1);
        }

        return getDataTable(list);
    }
    //根据商铺id查合同
    @PreAuthorize("@ss.hasPermi('feeDeposit:temporarydeposit:remove')")
    @GetMapping("/ShopIdByUser/{id}")
    public AjaxResult ShopIdByUser(@PathVariable("id") Long id)
    {
        List<ZhwyRentalcontract> zhwyRentalcontracts = zhwyRentalcontractMapper.selectZhwyRentalcontractByshopId(id);
        for(ZhwyRentalcontract zhwyRentalcontract:zhwyRentalcontracts){
            ZhwyHirer zhwyHirer = zhwyHirerMapper.selectZhwyHirerById(zhwyRentalcontract.getHirerId());
            zhwyRentalcontract.setHirerName(zhwyHirer.getName());
            zhwyRentalcontract.setHirerPhone(zhwyHirer.getPhone());
            return success(zhwyRentalcontract);
        }
        return success(zhwyRentalcontracts);
    }
    //根据商铺id查临时收费
    @PreAuthorize("@ss.hasPermi('feeDeposit:temporarydeposit:remove')")
    @GetMapping("/ShopIdBymoney1/{id}")
    public AjaxResult ShopIdBymoney(@PathVariable("id") Long id)
    {
        ZhwyTemporarydeposit zhwyTemporarydeposit =new ZhwyTemporarydeposit();
        zhwyTemporarydeposit.setShopsId(id);
        return success(zhwyTemporarydepositService.selectZhwyTemporarydepositList(zhwyTemporarydeposit));
    }
    //根据商铺id查押金管理
    @PreAuthorize("@ss.hasPermi('feeDeposit:temporarydeposit:remove')")
    @GetMapping("/ShopIdBymoney2/{id}")
    public AjaxResult ShopIdBymoney1(@PathVariable("id") Long id)
    {
        ZhwyTemporarydeposit zhwyTemporarydeposit =new ZhwyTemporarydeposit();
        zhwyTemporarydeposit.setShopsId(id);
        return success(zhwyTemporarydepositService.selectZhwyTemporarydepositList1(zhwyTemporarydeposit));
    }

    ///押金管理列表
    @PreAuthorize("@ss.hasPermi('feeDeposit:temporarydeposit:list1')")
    @GetMapping("/list1")
    public TableDataInfo list1(ZhwyTemporarydeposit zhwyTemporarydeposit)
    {
        startPage();
        List<ZhwyTemporarydeposit> list = zhwyTemporarydepositService.selectZhwyTemporarydepositList1(zhwyTemporarydeposit);
        return getDataTable(list);
    }

    /**
     * 导出临时收费/押金管理列表
     */
    @PreAuthorize("@ss.hasPermi('feeDeposit:temporarydeposit:export')")
    @Log(title = "临时收费/押金管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZhwyTemporarydeposit zhwyTemporarydeposit)
    {
        List<ZhwyTemporarydeposit> list = zhwyTemporarydepositService.selectZhwyTemporarydepositList(zhwyTemporarydeposit);
        ExcelUtil<ZhwyTemporarydeposit> util = new ExcelUtil<ZhwyTemporarydeposit>(ZhwyTemporarydeposit.class);
        util.exportExcel(response, list, "临时收费/押金管理数据");
    }

    /**
     * 获取临时收费/押金管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('feeDeposit:temporarydeposit:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(zhwyTemporarydepositService.selectZhwyTemporarydepositById(id));
    }

    /**
     * 新增临时收费/押金管理
     */
    @PreAuthorize("@ss.hasPermi('feeDeposit:temporarydeposit:add')")
    @Log(title = "临时收费/押金管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZhwyTemporarydeposit zhwyTemporarydeposit)
    {
        return toAjax(zhwyTemporarydepositService.insertZhwyTemporarydeposit(zhwyTemporarydeposit));
    }

    /**
     * 修改临时收费/押金管理
     */
    @PreAuthorize("@ss.hasPermi('feeDeposit:temporarydeposit:edit')")
    @Log(title = "临时收费/押金管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZhwyTemporarydeposit zhwyTemporarydeposit)
    {
        return toAjax(zhwyTemporarydepositService.updateZhwyTemporarydeposit(zhwyTemporarydeposit));
    }

    /**
     * 删除临时收费/押金管理
     */
    @PreAuthorize("@ss.hasPermi('feeDeposit:temporarydeposit:remove')")
    @Log(title = "临时收费/押金管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(zhwyTemporarydepositService.deleteZhwyTemporarydepositByIds(ids));
    }


//    根据时间查询数据
    @Anonymous
    @GetMapping("/time")
    public List selectByTime(ZhwyTemporarydeposit zhwyTemporarydeposit){
        System.out.println("+++++++++++++"+zhwyTemporarydeposit.getChargeTime());
        List<ZhwyTemporarydeposit> list = zhwyTemporarydepositService.selectByTime(zhwyTemporarydeposit.getChargeTime());
        return list;
    }
    @Anonymous
    @GetMapping("/time1")
    public List selectByTime1(ZhwyTemporarydeposit zhwyTemporarydeposit){
        System.out.println("---------------"+zhwyTemporarydeposit.getChargeTime());
        List<ZhwyTemporarydeposit> list = zhwyTemporarydepositService.selectByTime1(zhwyTemporarydeposit.getChargeTime());
        return list;
    }

    @Anonymous
    @GetMapping("/time2")
    public List selectByTime2(ZhwyTemporarydeposit zhwyTemporarydeposit){
        List<ZhwyTemporarydeposit> list = zhwyTemporarydepositService.selectByTime2(zhwyTemporarydeposit.getChargeTime());
        return list;
    }
}
